package com.z.common.job.controller;

import com.github.pagehelper.PageInfo;
import com.z.common.job.entity.ScheduleJob;
import com.z.common.job.service.IScheduleJobService;
import com.z.common.utils.SequenceUtil;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import static com.z.common.web.vo.response.Result.decide;
import static com.z.common.web.vo.response.Result.success;

/**
 * Describe: 定时任务控制器
 * Author: 就眠仪式
 * createTime: 
 */
@RestController
@Api(tags = {"定时任务"})
@RequestMapping("/system/job")
public class ScheduleJobController extends BaseController {

    /**
     * Describe: 定时任务服务
     */
    @Resource
    private IScheduleJobService scheduleJobService;

    /**
     * Describe: 获取定时任务列表视图
     * Param ModelAndView
     * Return 定时任务列表视图
     */
    @GetMapping("main")
    @PreAuthorize("hasPermission('/system/job/main','sys:job:main')")
    public ModelAndView main() {
        return jumpPage("system/job/main");
    }

    /**
     * Describe: 获取定时任务列表数据
     * Param PageVo
     * Return 定时任务列表数据
     */
    @GetMapping("data")
    @PreAuthorize("hasPermission('/system/job/data','sys:job:main')")
    public Result data(PageVo pageDomain, ScheduleJob param) {
        PageInfo<ScheduleJob> pageInfo = scheduleJobService.page(param, pageDomain);
        return Result.table(pageInfo);
    }

    /**
     * Describe: 获取定时任务新增视图
     * Param ModelAndView
     * Return ModelAndView
     */
    @GetMapping("add")
    @PreAuthorize("hasPermission('/system/job/add','sys:job:add')")
    public ModelAndView add() {
        return jumpPage("system/job/add");
    }

    /**
     * Describe: 获取定时任务修改视图
     * Param ModelAndView
     * Return ModelAndView
     */
    @GetMapping("edit")
    @PreAuthorize("hasPermission('/system/job/edit','sys:job:edit')")
    public ModelAndView edit(Model model, String jobId) {
        model.addAttribute("scheduleJob", scheduleJobService.getById(jobId));
        return jumpPage("system/job/edit");
    }

    /**
     * Describe: 保存定时任务数据
     * Param ScheduleJob
     * Return Result
     */
    @RequestMapping("/save")
    @PreAuthorize("hasPermission('/system/job/add','sys:job:add')")
    public Result save(@RequestBody ScheduleJob scheduleJob) {
        scheduleJob.setJobId(SequenceUtil.makeStringId());
        scheduleJob.setCreateTime(LocalDateTime.now());
        Boolean result = scheduleJobService.save(scheduleJob);
        return decide(result);
    }

    /**
     * Describe: 执行一次定时任务
     * Param ScheduleJob
     * Return Result 执行结果
     */
    @RequestMapping("/run")
    @PreAuthorize("hasPermission('/system/job/run','sys:job:run')")
    public Result run(String jobId) {
        scheduleJobService.run(jobId);
        return success("运行成功");
    }

    /**
     * Describe: 更新定时任务数据
     * Param ScheduleJob
     * Return Result
     */
    @RequestMapping("/update")
    @PreAuthorize("hasPermission('/system/job/edit','sys:job:edit')")
    public Result update(@RequestBody ScheduleJob scheduleJob) {
        Boolean result = scheduleJobService.update(scheduleJob);
        return decide(result);
    }

    /**
     * Describe: 停止定时任务
     * Param jobId
     * Return Result 执行结果
     */
    @PutMapping("/pause")
    @PreAuthorize("hasPermission('/system/job/pause','sys:job:pause')")
    public Result pauseJob(String jobId) {
        Boolean result = scheduleJobService.pause(jobId);
        return decide(result);
    }

    /**
     * Describe: 恢复定时任务
     * Param: jobId
     * Return: 恢复定时任务
     */
    @RequestMapping("/resume")
    @PreAuthorize("hasPermission('/system/job/resume','sys:job:resume')")
    public Result resumeJob(String jobId) {
        Boolean result = scheduleJobService.resume(jobId);
        return decide(result);
    }

    /**
     * Describe: 删除定时任务
     * Param: jobId
     * Return Result
     */
    @RequestMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/system/job/remove','sys:job:del')")
    public Result deleteJob(@PathVariable("id") String jobId) {
        Boolean result = scheduleJobService.delete(jobId);
        return decide(result);
    }
}
